<?php
/**
 * @var \yii\web\View $this
 * @var \common\models\db\AdminPower[] $powerTree
 * @var string $nst
 * @var boolean $isParentLast
 */
use fztadmin\modules\rbac\widgets\power_tree_list\PowerTreeList;
use common\models\db\AdminPower;
use igorvolnyi\widgets\modal\ModalAjaxMultiple;
use yii\helpers\Url;
use wodrow\yii2wtools\tools\ArrayHelper;
use yii\bootstrap\Html;
?>

<div>
    <?php foreach($powerTree as $k => $v): ?>
        <?php
        $pathArr = ArrayHelper::str2arr($v['path'], '-');
        $pathArrTotal = count($pathArr);
        $listTotal = count($powerTree);
        $level = count($pathArr);
        $isLast = $listTotal == $k + 1;
        $ns = $nst."2";
        $nshow = $ns;
        $nshow = str_replace("0", "&nbsp;&nbsp;&nbsp;&nbsp;", $nshow);
        $nshow = str_replace("1", "|&nbsp;&nbsp;&nbsp;", $nshow);
        $nshow = str_replace("2", "|_&nbsp;", $nshow);
        switch ($v['type']){
            case AdminPower::TYPE_MENU:
                $iconClass = "glyphicon glyphicon-folder-open";
                break;
            default:
                $iconClass = "glyphicon glyphicon-file";
                break;
        }
        ?>
        <tr>
            <td>
                <?=$nshow ?>
                <?=Html::icon($iconClass) ?>
                <?=$v['name'] ?>
            </td>
            <td><?=AdminPower::instance()->getTypeDesc()[$v['type']] ?></td>
            <td><?=$v['route'] ?></td>
            <td><?=$v['sort'] ?></td>
            <td>
                <?php
                if ($v['type'] == AdminPower::TYPE_MENU){
                    echo ModalAjaxMultiple::widget([
                        'header' => "[{$v['name']}]新增子权限",
                        'toggleButton' => [
                            'label' => "<i class='glyphicon glyphicon-plus'></i>",
                            'class' => 'btn btn-primary btn-xs'
                        ],
                        'url' => Url::to(['/rbac/power/new', 'pid' => $v['id']]), // Ajax view with form to load
                        'ajaxSubmit' => true, // Submit the contained form as ajax, true by default
                        'options' => ['class' => 'header-primary'],
                        'autoClose' => true,
                    ]);
                }
                ?>
                <?php
                echo ModalAjaxMultiple::widget([
                    'header' => "[{$v['name']}]修改",
                    'toggleButton' => [
                        'label' => "<i class='glyphicon glyphicon-pencil'></i>",
                        'class' => 'btn btn-warning btn-xs'
                    ],
                    'url' => Url::to(['/rbac/power/edit', 'id' => $v['id']]), // Ajax view with form to load
                    'ajaxSubmit' => true, // Submit the contained form as ajax, true by default
                    'options' => ['class' => 'header-primary'],
                    'autoClose' => true,
                ]);
                ?>
                <?php
                if (!($v['_child'] ?? null)){
                    echo Html::a("<i class='glyphicon glyphicon-minus'></i>", ['/rbac/power/del', 'id' => $v['id']], ['class' => 'btn btn-danger btn-xs', 'data-method' => 'post','data-confirm' => "确认删除[{$v['name']}]?",]);
                }
                ?>
            </td>
        </tr>
        <?php if($v['_child'] ?? null): ?>
            <?php
            if ($isLast){
                $next = $nst."0";
            }else{
                $next = $nst."1";
            }
            ?>
            <?=PowerTreeList::widget([
                'powerTree' => $v['_child'],
                'nst' => $next,
            ]) ?>
        <?php endif; ?>
    <?php endforeach; ?>
</div>
